package kr.couponplay.android.activity.deal.category;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.Category;
import kr.couponplay.android.activity.R;
import kr.couponplay.android.activity.deal.DealListActivity;
import kr.couponplay.android.activity.deal.menu.SubMenuHelper;
import kr.couponplay.android.activity.deal.menu.TopMenuHelper;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class CategoryListFragment extends ListFragment implements OnChildClickListener, OnGroupExpandListener {
	private DealListActivity activity;
	private CategoryExpandableAdapter categoryExpandableAdapter;
	private ExpandableListView listView;
	
	public static CategoryListFragment newInstance(DealListActivity activity) {
		CategoryListFragment fragment = new CategoryListFragment();
		fragment.activity = activity;
		return fragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View onCreateView = inflater.inflate(R.layout.category_fragment, container, false);
		
		categoryExpandableAdapter = new CategoryExpandableAdapter(getActivity(), AppContext.getMainCategories(getActivity()));
		listView = (ExpandableListView) onCreateView.findViewById(android.R.id.list);
		listView.setGroupIndicator(null);
		listView.setOnChildClickListener(this);
		listView.setOnGroupExpandListener(this);

		listView.setAdapter(categoryExpandableAdapter);
		
		CategoryHelper.listView = listView;
		CategoryHelper.draw(TopMenuHelper.selectedTopMenuButtonIndex);
		
		return onCreateView;
	}
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
		Category category = categoryExpandableAdapter.getChild(groupPosition, childPosition);
		String categoryName = category.getName();
		
		if (category.getLevelNo() == 1) {
			CategoryHelper.mainCategoryId = category.getId();
			CategoryHelper.subCategoryId = 0;
			categoryName = activity.getResources().getString(R.string.all_deal_name);
		} else {
			CategoryHelper.mainCategoryId = category.getParentId();
			CategoryHelper.subCategoryId = category.getId();
		}
		
//		view.setBackgroundColor(Color.DKGRAY);
		TopMenuHelper.selectTopMenuButton(groupPosition);
		SubMenuHelper.selectSubMenuButton(0);
		SubMenuHelper.setCategorySubMenuText(categoryName);
		activity.toggle();
		activity.getDealList();
		return true;
	}
	
    @Override
    public void onGroupExpand(int groupPosition) {
        int groupCount = categoryExpandableAdapter.getGroupCount();
         
        // 한 그룹을 클릭하면 나머지 그룹들은 닫힌다.
        for (int i = 0; i < groupCount; i++) {
            if (i != groupPosition) {
            	listView.collapseGroup(i);
            }
        }
    }
	
//	@Override
//	public void onListItemClick(ListView l, View v, int position, long id) {
//        super.onListItemClick(l, v, position, id);
//		Category category = categoryAdapter.getItem(position);
//		String categoryName = category.getName();
//		
//		if (category.getLevelNo() == 1) {
//			CategoryHelper.mainCategoryId = category.getId();
//			CategoryHelper.subCategoryId = 0;
//			categoryName = activity.getResources().getString(R.string.all_deal_name);
//		} else {
//			CategoryHelper.mainCategoryId = category.getParentId();
//			CategoryHelper.subCategoryId = category.getId();
//		}
//		
//		SubMenuHelper.selectSubMenuButton(0);
//		SubMenuHelper.setCategorySubMenuText(categoryName);
//		activity.toggle();
//		activity.getDealList();
//    }
}
